//
//  JTEssenceViewController.m
//  百思不得姐
//
//  Created by sun lei on 16/11/17.
//  Copyright © 2016年 BeiJingWangDianJieTong. All rights reserved.
//

#import "JTEssenceViewController.h"
#import "JTTitleButton.h"

/** 子控制器 */
#import "JTAllTableViewController.h"
#import "JTVideoTableViewController.h"
#import "JTVoiceTableViewController.h"
#import "JTPictureTableViewController.h"
#import "JTWordTableViewController.h"

/** 推荐标签 */
#import "JTRecommendTagTableViewController.h"

@interface JTEssenceViewController () <UIScrollViewDelegate>

/** 当前选中的按钮 */
@property (nonatomic, weak) JTTitleButton *selectedTitleButton;
/** 标题按钮下的指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** UIScrollView */
@property (nonatomic, weak) UIScrollView *scrollView;
/** 标题栏 */
@property (nonatomic, weak) UIView *titleView;

@end

@implementation JTEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavigationController];
    
    [self setupChildViewController];
    
    [self setupScrollView];
    
    [self setupTitleView];
    
    // 默认添加子控制器的view
    [self addChildVCView];
}

- (void)setupNavigationController {
    
    [self.view setBackgroundColor:JTCommonBgColor];
    // 顶部的标题
    [self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]]];
    // 左边
    [self.navigationItem setLeftBarButtonItem:[UIBarButtonItem jt_itemWithImage:@"MainTagSubIcon" highLightedImage:@"MainTagSubIconClick" target:self action:@selector(tagClick:)]];
}

- (void)setupChildViewController {
    
    JTAllTableViewController *allTVC = [[JTAllTableViewController alloc] init];
    [self addChildViewController:allTVC];
    
    JTVideoTableViewController *videoTVC = [[JTVideoTableViewController alloc] init];
    [self addChildViewController:videoTVC];
    
    JTPictureTableViewController *pictureTVC = [[JTPictureTableViewController alloc] init];
    [self addChildViewController:pictureTVC];
    
    JTWordTableViewController *wordTVC = [[JTWordTableViewController alloc] init];
    [self addChildViewController:wordTVC];
    
    JTVoiceTableViewController *voiceTVC = [[JTVoiceTableViewController alloc] init];
    [self addChildViewController:voiceTVC];
}

- (void)setupScrollView {
    
    // 不允许自动调整scrollView的内边距(要保证tableView的frame是整个屏幕，穿透效果)
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [scrollView setDelegate:self];
    [scrollView setBackgroundColor:JTCommonBgColor];
    [scrollView setPagingEnabled:YES];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    // 添加所有子控制器的view到scrollView中去
    [scrollView setContentSize:CGSizeMake(self.childViewControllers.count * scrollView.jt_width, 0)];
    [self.view addSubview:scrollView];
    [self setScrollView:scrollView];
}

- (void)setupTitleView {
    
    // 标题栏
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.jt_width, 35)];
    [titleView setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:0.7]];
    [self.view addSubview:titleView];
    [self setTitleView:titleView];
    
    // 添加标题
    NSArray *titles = @[@"全部", @"视频", @"图片", @"段子", @"声音"];
    NSUInteger count = titles.count;
    CGFloat titleButtonWidth = self.view.jt_width / count;
    CGFloat titltButtonHeight = titleView.jt_height;
    for (NSUInteger i = 0; i < count; i++) {
        
        // 创建
        JTTitleButton *titleButton = [JTTitleButton buttonWithType:UIButtonTypeCustom];
        [titleButton setTag:i];
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:titleButton];
        
        // 设置数据
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        
        // 设置frame
        [titleButton setFrame:CGRectMake(titleButtonWidth * i, 0, titleButtonWidth, titltButtonHeight)];
    }
    
    // 按钮的选中颜色
    JTTitleButton *firstTitleButton = titleView.subviews.firstObject;
    
    // 底部的指示器
    UIView *indicatorView = [[UIView alloc] init];
    [indicatorView setBackgroundColor:[firstTitleButton titleColorForState:UIControlStateSelected]];
    [indicatorView setJt_height:1.0];
    [indicatorView setJt_y:titleView.jt_height - indicatorView.jt_height];
    [titleView addSubview:indicatorView];
    [self setIndicatorView:indicatorView];
    
    /* 默认情况：选中最前面的按钮 */
    [firstTitleButton setSelected:YES];
    [self setSelectedTitleButton:firstTitleButton];
    // 立刻根据文字内容计算label的宽度
    [firstTitleButton.titleLabel sizeToFit];
    // 设置按钮指示器的宽高
    [self.indicatorView setJt_width:firstTitleButton.titleLabel.jt_width];
    [self.indicatorView setJt_centerX:firstTitleButton.jt_centerX];
}

#pragma mark - 监听点击
- (void)tagClick:(JTTitleButton *)sender {
    
    JTRecommendTagTableViewController *controller = [[JTRecommendTagTableViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)titleClick:(JTTitleButton *)titleButton {
    
    // 某个标题按钮被重复点击了
    if (titleButton == self.selectedTitleButton) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:JTTitleButtonDidRepeatClickNotification object:nil];
        return;
    }
    
    [self.selectedTitleButton setSelected:NO];
    [titleButton setSelected:YES];
    [self setSelectedTitleButton:titleButton];
    
    // 指示器
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.indicatorView setJt_width:titleButton.titleLabel.jt_width];
        [self.indicatorView setJt_centerX:titleButton.jt_centerX];
    }];
    
    // 让UIScrollView滚动到对应位置
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = titleButton.tag * self.scrollView.jt_width;
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark - 添加子控制器的view
- (void)addChildVCView {
    
    /* 添加子控制器的view到scrollView中 */
    // 子控制器的索引
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.jt_width;
    
    // 取出子控制器
    UIViewController *childVC = self.childViewControllers[index];
    
    // 判断是否已经添加过了(如果已经添加过了，那就不添加了)
    if ([childVC isViewLoaded]) return;
    
    [childVC.view setFrame:self.scrollView.bounds];
    
    [self.scrollView addSubview:childVC.view];
}

#pragma mark - <UIScrollViewDelegate>
/**
 在scrollView滚动动画结束时，就会调用这个方法
 前提：使用setContentOffset:animated:或者scrollRectVisible:animated:方法使scrollView开始了滚动动画
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    [self addChildVCView];
}

/**
 在scrollView滚动动画结束时，就会调用这个方法
 前提：人为拖拽scrollView产生的滚动动画
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 选中\点击对应的按钮
    // 1.按钮的索引
    NSUInteger index = scrollView.contentOffset.x / scrollView.jt_width;
    JTTitleButton *titleButton = self.titleView.subviews[index];
    [self titleClick:titleButton];
    
    // 2.添加子控制器的view
    [self addChildVCView];
}

@end
